package com.ias.common.utils.json;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JsonUtil使用示例
 * 
 * @author IAS
 * @since 1.0.0
 */
public class JsonUtilExample {

    public static void main(String[] args) {
        System.out.println("=== JsonUtil 使用示例 ===\n");

        // 1. 对象转JSON字符串
        demonstrateObjectToJson();

        // 2. JSON字符串转对象
        demonstrateJsonToObject();

        // 3. 集合操作
        demonstrateCollectionOperations();

        // 4. Map操作
        demonstrateMapOperations();

        // 5. JsonNode操作
        demonstrateJsonNodeOperations();

        // 6. 字节数组操作
        demonstrateBytesOperations();

        // 7. 工具方法
        demonstrateUtilityMethods();

        // 8. JSON合并
        demonstrateJsonMerge();
    }

    /**
     * 演示对象转JSON字符串
     */
    private static void demonstrateObjectToJson() {
        System.out.println("1. 对象转JSON字符串");
        System.out.println("==================");

        User user = new User("张三", 25, "zhangsan@example.com");
        
        // 普通JSON字符串
        String json = JsonUtil.toJsonString(user);
        System.out.println("普通JSON: " + json);

        // 格式化JSON字符串
        String prettyJson = JsonUtil.toJsonStringPretty(user);
        System.out.println("格式化JSON: \n" + prettyJson);
        
        System.out.println();
    }

    /**
     * 演示JSON字符串转对象
     */
    private static void demonstrateJsonToObject() {
        System.out.println("2. JSON字符串转对象");
        System.out.println("==================");

        String json = "{\"name\":\"李四\",\"age\":30,\"email\":\"lisi@example.com\"}";
        
        // 转换为对象
        User user = JsonUtil.parseObject(json, User.class);
        System.out.println("转换后的用户: " + user);

        // 使用TypeReference
        String mapJson = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
        Map<String, String> map = JsonUtil.parseObject(mapJson, new TypeReference<Map<String, String>>() {});
        System.out.println("转换后的Map: " + map);
        
        System.out.println();
    }

    /**
     * 演示集合操作
     */
    private static void demonstrateCollectionOperations() {
        System.out.println("3. 集合操作");
        System.out.println("===========");

        List<User> users = Arrays.asList(
            new User("用户1", 20, "user1@example.com"),
            new User("用户2", 25, "user2@example.com")
        );

        // List转JSON
        String listJson = JsonUtil.toJsonString(users);
        System.out.println("List转JSON: " + listJson);

        // JSON转List
        List<User> parsedUsers = JsonUtil.parseList(listJson, User.class);
        System.out.println("JSON转List: " + parsedUsers);
        
        System.out.println();
    }

    /**
     * 演示Map操作
     */
    private static void demonstrateMapOperations() {
        System.out.println("4. Map操作");
        System.out.println("==========");

        String json = "{\"name\":\"测试\",\"age\":30,\"active\":true,\"score\":95.5}";

        // 转换为Map<String, Object>
        Map<String, Object> map = JsonUtil.parseMap(json);
        System.out.println("转换后的Map: " + map);

        // 转换为指定类型的Map
        String stringMapJson = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
        Map<String, String> stringMap = JsonUtil.parseMap(stringMapJson, String.class, String.class);
        System.out.println("指定类型Map: " + stringMap);
        
        System.out.println();
    }

    /**
     * 演示JsonNode操作
     */
    private static void demonstrateJsonNodeOperations() {
        System.out.println("5. JsonNode操作");
        System.out.println("===============");

        String json = "{\"user\":{\"name\":\"王五\",\"age\":35},\"items\":[\"item1\",\"item2\"]}";
        JsonNode node = JsonUtil.parseJsonNode(json);

        if (node != null) {
            System.out.println("用户名: " + node.get("user").get("name").asText());
            System.out.println("年龄: " + node.get("user").get("age").asInt());
            System.out.println("第一个物品: " + node.get("items").get(0).asText());
        }
        
        System.out.println();
    }

    /**
     * 演示字节数组操作
     */
    private static void demonstrateBytesOperations() {
        System.out.println("6. 字节数组操作");
        System.out.println("===============");

        User user = new User("赵六", 40, "zhaoliu@example.com");

        // 对象转字节数组
        byte[] bytes = JsonUtil.toJsonBytes(user);
        System.out.println("字节数组长度: " + (bytes != null ? bytes.length : 0));

        // 字节数组转对象
        User parsedUser = JsonUtil.parseObject(bytes, User.class);
        System.out.println("从字节数组解析的用户: " + parsedUser);
        
        System.out.println();
    }

    /**
     * 演示工具方法
     */
    private static void demonstrateUtilityMethods() {
        System.out.println("7. 工具方法");
        System.out.println("===========");

        // 深度复制
        User original = new User("原始用户", 50, "original@example.com");
        User copy = JsonUtil.deepCopy(original, User.class);
        System.out.println("原始用户: " + original);
        System.out.println("复制用户: " + copy);
        System.out.println("是否为同一对象: " + (original == copy));

        // JSON格式验证
        System.out.println("有效JSON: " + JsonUtil.isValidJson("{\"name\":\"test\"}"));
        System.out.println("无效JSON: " + JsonUtil.isValidJson("{invalid}"));
        
        System.out.println();
    }

    /**
     * 演示JSON合并
     */
    private static void demonstrateJsonMerge() {
        System.out.println("8. JSON合并");
        System.out.println("===========");

        String mainJson = "{\"name\":\"主要用户\",\"age\":25,\"city\":\"北京\"}";
        String updateJson = "{\"age\":30,\"email\":\"updated@example.com\"}";

        JsonNode merged = JsonUtil.mergeJson(mainJson, updateJson);
        if (merged != null) {
            System.out.println("合并后的JSON: " + JsonUtil.toJsonString(merged));
        }
        
        System.out.println();
    }

    /**
     * 示例用户类
     */
    public static class User {
        private String name;
        private Integer age;
        private String email;
        private LocalDateTime createTime;

        public User() {
            this.createTime = LocalDateTime.now();
        }

        public User(String name, Integer age, String email) {
            this.name = name;
            this.age = age;
            this.email = email;
            this.createTime = LocalDateTime.now();
        }

        // Getters and Setters
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public LocalDateTime getCreateTime() {
            return createTime;
        }

        public void setCreateTime(LocalDateTime createTime) {
            this.createTime = createTime;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", email='" + email + '\'' +
                    ", createTime=" + createTime +
                    '}';
        }
    }
} 